package class08_前缀树不基于比较的排序排序的稳定性.test;

import java.util.Arrays;

/**
 * 基数排序
 */
public class Code04_RadixSort {
    public static void radixSort(int[] arr){
        if(arr == null || arr.length < 2){
            return;
        }
        radixSort(arr, 0, arr.length - 1, maxbits(arr));
    }

    private static void radixSort(int[] arr, int l, int r, int digit) {
        int[] help = new int[arr.length];
        for (int i = 1; i <= digit; i++) {//个 十 百
            int[] count = new int[10];
            for (int j = 0; j < arr.length; j++) {//初始化count
                int num = getDigit(arr[j], i);
                count[num]++;
            }
            for (int k = 1; k < count.length; k++) {
                count[k] = count[k] + count[k-1];
            }
            for (int m = arr.length - 1; m >= 0 ; m--) {
                int num = getDigit(arr[m], i);
                help[--count[num]] = arr[m];
                count[num] = count[num]--;
            }
            for (int n = 0; n < arr.length; n++) {
                arr[n] = help[n];
            }
        }

    }

    private static int getDigit(int num, int digit) {
        return (int)((num / Math.pow(10, digit - 1)) % 10);
    }

    private static int maxbits(int[] arr) {
        if(arr == null){
            return 0;
        }
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < arr.length; i++) {
            max = Math.max(max, arr[i]);
        }
        int ans = 0;
        while (max > 0){
            max = max / 10;
            ans ++;
        }
        return ans;
    }

    public static void main(String[] args) {
        int testTime = 500;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr);
            radixSort(arr);
            comparator(arr2);
            if(!isEqual(arr, arr2)){
                succeed = false;
                printArray(arr);
                printArray(arr2);
            }
        }
        System.out.println(succeed ? "succeed!!":"fuck!!");
        int[] arr = generateRandomArray(maxSize, maxValue);
        printArray(arr);
        radixSort(arr);
        printArray(arr);
    }

    private static void comparator(int[] arr2) {
        Arrays.sort(arr2);
    }

    private static void printArray(int[] arr) {
        if(arr == null){
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    private static boolean isEqual(int[] arr, int[] arr2) {
        if(arr == null && arr2 != null){
            return false;
        }
        if(arr != null && arr2 == null){
            return false;
        }
        if(arr.length != arr2.length){
            return false;
        }
        if(arr == null && arr2 == null){
            return true;
        }
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != arr2[i]){
                return false;
            }
        }
        return true;
    }

    private static int[] copyArray(int[] arr) {
        if(arr == null){
            return new int[0];
        }
        int[] help = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            help[i] = arr[i];
        }
        return help;
    }

    private static int[] generateRandomArray(int maxSize, int maxValue) {
        int size = (int)(Math.random() * maxSize + 1);
        int[] help = new int[size];
        for (int i = 0; i < size; i++) {
            help[i] = (int)(Math.random() * maxValue + 1);
        }
        return help;
    }
}
